<?php
App::import('Controller', 'Upload');
class UploadimportController extends UploadController {

     public function beforeFilter(){
		if(!$this->request->is('ajax'))
	        parent::beforeFilter();
         // $this->Auth->allow('remove');
        // don't require logging in for login and logout actions
        $this->loadModel('Import');
    }

    protected function save($file, $vars=null){
        $fileUploaded = null;
		try {
	        $fileUploaded = parent::save($file, $vars);
            $this->Import->create();
            $this->Import->set('attach_id',$fileUploaded['id']);
            $this->Import->set('type',substr($file['name'],0,1));
            $this->Import->save();
			return $fileUploaded;
		} catch (Exception $e1) {
	    	CakeLog::error($e1->getMessage());
			try {
	            $this->Attach->read(null, $fileUploaded["id"]);
				$thumb = $this->Attach->attachTipo->data["AttachTipo"]["thumb"];
				$this->Attach->deleteFile();
		        if ($thumb) {
		            try {
		                $this->Thumb->deleteThumb($this->Attach->getAbsolutePath(), $this->Attach->filename);
		            } catch (Exception $e3) {
				    	CakeLog::error($e3->getMessage());
		                throw $e3;
		            }
		        }
			} catch (Exception $e2) {
		    	CakeLog::error($e2->getMessage());
                throw $e2;
			}
			throw $e1;
		} 
	}
    
        
    protected function remove($vars){
        $this->loadModel("JobberAttach");
        $idFoto = $vars[0];
        $idJobber = $vars[1];
        
        $this->JobberAttach->recursive = -1;
        if(!$this->JobberAttach->deleteAll(array('JobberAttach.jobber_id' => $idJobber,'JobberAttach.attach_id'=> $idFoto))){
            CakeLog::error("Errore nella cancellazione dell'AttachJobber con idFoto:".$idFoto." e idJobber".$idJobber);
            throw new Exception(__("Errore nella cancellazione dell'Attach %s.",$idFoto));
        }
    }
 
	
	
	
}
?>